<?php
//命名空间
namespace Home\Controller;

class IndexController extends SimpleController {

    public function _initialize(){
        $banner = M('banner')->select();
        $this->assign('banner',$banner);
        // 实例化用户扩展信息并获取头像信息
        if (!empty($_SESSION['user'])) {
            $uid = $_SESSION['user']['uid'];
            $photo_info = M('user_info')->where('uid =' .$uid)->find();
            $this->assign('path',$photo_info['path']);
            $this->assign('thumbpath',$photo_info['thumbpath']);
            $user = M('user')->where('id ='.$uid)->find();

            $this->assign('fans',$photo_info['fans']);
            $this->assign('faned',$photo_info['faned']);
            $this->assign('gold',$photo_info['gold']);
            $exp = $photo_info['exp'];

            $level = (($exp - $exp%100)/100 + 1);
            $exp = $exp%100;
            switch ($level) {
                case '1':
                    $design = '初学乍练';
                    break;
                case '2':
                    $design = '登堂入室';
                    break;
                case '3':
                    $design = '初窥堂奥';
                    break;
                case '4':
                    $design = '略有小成';
                    break;
                case '5':
                    $design = '渐入佳境';
                    break;
                case '6':
                    $design = '炉火纯青';
                    break;
                case '7':
                    $design = '自成一派';
                    break;
                case '8':
                    $design = '功行圆满';
                    break;
                case '9':
                    $design = '登峰造极';
                    break;
                default:
                    $design = '神功绝世';
                    break;
            }
            $this->assign('design',$design);
            $this->assign('level',$level);
            // 如果用户昵称为空,就把用户名当用户昵称显示
            if ($photo_info['nickname'] == null) {
                $photo_info['nickname'] = $user['name'];
                
            }
            $this->assign('user',$user);
            $this->assign('user_info',$photo_info);
        }
        
    }
    public function index(){
        
        // 如果用户已经登录,就首页展示的用户信息
        // if (!empty($_SESSION['user'])) {
        //     $id = $_SESSION['user']['uid'];
        //     $user_info = M('user_info') ->where("uid = '$id'")->find();
        //     $this->assign('user_info',$user_info);
        // }

        //本周热门
        $mode = M('experience');
        $week = date(w);
        $hour = date(H);
        $minute = date(i);
        $second = date(s);
        if ($week == 0) {$week = 7;}
        $time = (time() -(($week-1)*86400)-($hour *3600  + $minute * 60 + $second));
        $data = $mode->where('create_time > '.$time)->order('reader desc')->limit(9)->select();
        if (empty($data)){$data = null;}
        $this->assign('experience',$data);

        
        //最新经验
        $new_data = $mode->order('id desc')->limit(6)->select();

        $this->assign('new_experience',$new_data);

        $this->assign('title','[摆渡社区]--欢迎来到摆渡社区--摆渡社区欢迎您的加入');

        //我的经验
        if (!empty($_SESSION['user']['uid'])) {
            $my_experience = $mode->where('uid = '.$_SESSION['user']['uid'])->order('id desc')->limit(4)->select();
            $this->assign('my_experience',$my_experience);
        }

        //分享达人
        $people_data = $mode->field('uid,count(uid)')->order('count(uid) desc')->limit(3)->group('uid')->select();
        $user_info = array();
        foreach ($people_data as $key => $val) {
            $model = M('user')->field('name')->where('id = '.$val['uid'])->find();
            $model_path = M('user_info')->field('thumbpath,fans,signature,uid')->where('uid = '.$val['uid'])->find();
            // print_r($model);
            $info = array_merge($model,$model_path);
            $user_info[] = $info;
        }
        $this->assign('show_man',$user_info);
        $this->display();

    }

    // 签到
    public function register()
    {
        if (!IS_AJAX) {
            $this->error('亲,请不要胡来好吗....');
        }
        $data['uid'] = $_SESSION['user']['uid'];
        $data['date'] = date('Ymd');
        $register =D('register');
        // 判断用户是否已经签到
        if ($register->where($data)->find() != null) {
            $json['msg'] = '您已经签过到了';
            json_encode($json);
            $this->ajaxReturn($json);
            exit();
        }else {
            $register->add($data);
            // 更新用户数据
            $user_info = D('user_info');
            $user_info->where('uid ='.$data['uid'])->setInc('exp',10);
            $user_info->where('uid ='.$data['uid'])->setInc('gold',5);
            $_SESSION['user']['register'] = 'registered';
            $_SESSION['user']['register_num'] += 1;
            $json['msg'] = '恭喜您签到成功,经验+10,金币+5!';
            $json['num'] = $register->where('uid ='.$_SESSION['user']['uid'])->count();
            $json['gold'] = $user_info->where('uid ='.$_SESSION['user']['uid'])->find()['gold'];
            json_encode($json);
            $this->ajaxReturn($json);
        }


    }

    // 登出
    public function logout()
    {
        session('user',null);
        $this->success('注销成功,自动跳转至首页!',U('Home/Index/index'));
    }

    public function hot_experience()
    {
        $offset = $_GET['offset'];

        $offset = $offset * 9;
        $mode = M('experience');
        $week = date(w);
        $hour = date(H);
        $minute = date(i);
        $second = date(s);
        if ($week == 0) {$week = 7;}
        $time = (time() -(($week-1)*86400)-($hour *3600  + $minute * 60 + $second));
        $data = $mode->where('create_time > '.$time)->order('reader desc')->limit($offset.',9')->select();
        // if (empty($data)) {
        //     $data = $mode->where('create_time > '.$time)->order('g_count desc')->limit('0,9')->select();
        // }
        echo json_encode($data);
    }
    public function new_experience()
    {
        $mode = M('experience');
        $offset = $_GET['offset2'];
        $offset = $offset * 3 + 3;
        $new_data = $mode->order('id desc')->limit($offset.',3')->select();
        if (empty($new_data)){$new_data = null;}
        echo json_encode($new_data);
    }
}